package com.shengqin.config;

import com.shengqin.entity.School;
import com.shengqin.service.ISchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.*;

@Configuration
@Slf4j
public class InitConfigData implements CommandLineRunner {

    @Resource
    private ISchoolService schoolService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        redisTemplate.delete(RedisKeys.SCHOOL_MAP);
        List<School> schoolList = schoolService.list();
        Map<String, Object> schoolMap = new HashMap<>();
        schoolList.forEach(school -> {
            schoolMap.put(school.getCreateUserId() + "_" + school.getName().trim(), school);
        });
        redisTemplate.opsForHash().putAll(RedisKeys.SCHOOL_MAP, schoolMap);
        log.info("初始化学校数据至缓存");
    }
}
